﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using JUtil;

namespace CodeGen
{
    public class MasterViewSql
    {
        public string GetOrderBy;
        public string GetSelectSQLNoOrderBy;
    }

    public class DbSettings
    {
        public DatabaseMode DatabaseMode;
        public string ConnectionString;
    }

    public class MasterViewGenerator
    {
        public static void Test()
        {
            MasterViewGenerator generator = new MasterViewGenerator(
                @"R:\",
                 "TPPDDB.FestivalTicketSystem",
                 "FestivalTicketManagement",
                 "FESTIVAL_ACTIVITY",
                 new DbSettings
                 {
                     DatabaseMode = DatabaseMode.Oracle,
                     ConnectionString = "Data Source=tppddb;User ID=tpdbuser;Password=tpdbuser;"
                 },
                 new MasterViewSql
                 {
                     GetOrderBy = "ORDER BY ACTIVITY_ID DESC",
                     GetSelectSQLNoOrderBy = "SELECT * FROM FESTIVAL_ACTIVITY",
                 }
                );

            generator.Export();
        }

        public MasterViewGenerator(
            string ExportDirectory, 
            string NameSpace,
            string AspxName, 
            string DalName,
            DbSettings DbSettings,
            MasterViewSql MasterViewSql)
        {
            this.ExportDirectory = ExportDirectory;
            this.NameSpace = NameSpace;
            this.AspxName = AspxName;
            this.DalName = DalName;
            this.BllName = "BLL_" + DalName;
            this.DbSettings = DbSettings;
            this.MasterViewSql = MasterViewSql;

            AspxPath = JUtil.Path.File.GetAbsolutePath(ExportDirectory, AspxName + ".aspx");
            AspxCsPath = JUtil.Path.File.GetAbsolutePath(ExportDirectory, AspxName + ".aspx.cs");
            BllPath = JUtil.Path.File.GetAbsolutePath(ExportDirectory, BllName + ".cs");
            DalPath = JUtil.Path.File.GetAbsolutePath(ExportDirectory, DalName + ".cs");
            SqlPath = JUtil.Path.File.GetAbsolutePath(ExportDirectory, BllName + ".sql");
        }

        //GridView grv;

        string ExportDirectory;
        string NameSpace;
        string AspxName;
        string DalName;
        string BllName;

        DbSettings DbSettings;
        MasterViewSql MasterViewSql;

        private string AspxPath = string.Empty;
        private string AspxCsPath = string.Empty;
        private string BllPath = string.Empty;
        private string DalPath = string.Empty;
        private string SqlPath = string.Empty;

        public void Export()
        {
            ExportAspx();
            ExportAspxCs();
            ExportBll();
            ExportDal();
            ExportSql();
        }

        private void ExportAspx()
        {
            string filepath = AspxPath;

            string TypeName = string.Format("{0}.{1}", NameSpace, BllName);
            string DataObjectTypeName = string.Format("{0}.{1}", NameSpace, DalName);

            List<ColumnInfo> lstColumnInfo = new List <ColumnInfo>();
            foreach (FieldSchema item in lstFieldSchema)
            {
                ColumnInfo info = new ColumnInfo();
                info.HeaderText = item.ColumnName;
                info.BindingField = item.ColumnName;
                lstColumnInfo.Add(info);
            }

            MasterViewAspxFileGenerator generator = new MasterViewAspxFileGenerator(
                "Master",
                TypeName, 
                DataObjectTypeName, 
                lstColumnInfo);

            generator.Export(filepath);
        }
        private void ExportAspxCs()
        {
            string filepath = AspxCsPath;

            MasterViewAspxCsFileGenerator generator = new MasterViewAspxCsFileGenerator(
                NameSpace,
                AspxName,
                "Master",
                BllName);

            generator.Export(filepath);
        }

        #region ExportBll
        private void ExportBll()
        {
            string filepath = BllPath;

            BllFileGenerator generator = new BllFileGenerator(NameSpace, BllName, DalName, DbSettings);

            generator.Export(filepath);
        }
        #endregion
        #region ExportDal
        private void ExportDal()
        {
            /*
            string filepath = DalPath;

            List<PropertyInfo> lstPropertyInfo = new List<PropertyInfo>();
            foreach (FieldSchema item in lstFieldSchema)
            {
                PropertyInfo info = new PropertyInfo();
                info.propertyType = item.DataType;
                info.propertyName = item.ColumnName;
                info.propertyComment = string.Empty;
                lstPropertyInfo.Add(info);
            }

            DalFileGenerator generator = new DalFileGenerator(NameSpace, DalName, lstPropertyInfo);

            generator.Export(filepath);
            */
        }
        #endregion
        #region ExportSql
        private void ExportSql()
        {
            string filepath = SqlPath;

            SqlFile generator = new SqlFile(
                MasterViewSql.GetOrderBy,
                MasterViewSql.GetSelectSQLNoOrderBy,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty);

            generator.Export(filepath);
        }
        #endregion

        private List<FieldSchema> _lstFieldSchema = null;
        private List<FieldSchema> lstFieldSchema
        {
            get
            {
                if (_lstFieldSchema == null)
                {
                    IDatabase database = new Database(DbSettings.DatabaseMode, DbSettings.ConnectionString);

                    string sql = string.Format("{0} {1}", MasterViewSql.GetSelectSQLNoOrderBy, MasterViewSql.GetOrderBy);

                    _lstFieldSchema = FieldSchema.GetTableSchema(database, sql);
                }
                return _lstFieldSchema;
            }
        }
    }
}
